package com.example.springbatch.config;

import com.example.springbatch.entity.Person;
import com.example.springbatch.writer.CustomJdbcBatchItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
//@EnableBatchProcessing(dataSourceRef = "batchDataSource", transactionManagerRef = "batchTransactionManager")
public class MyJobConfiguration {
	/*@Primary
	@Qualifier("batchDataSource")
	@Bean
	public DataSource batchDataSource() {
		return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
				.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
				.generateUniqueName(true).build();
	}

	@Qualifier("appDataSource")
	@Bean
	public DataSource appDataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("dm.jdbc.driver.DmDriver");
		dataSource.setUrl("jdbc:dm://10.111.14.66:5236?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8");
		dataSource.setUsername("ZCPT");
		dataSource.setPassword("dianxinB310");
		return dataSource;
	}

	@Bean
	public JdbcTransactionManager batchTransactionManager(@Qualifier("batchDataSource")DataSource batchDataSource) {
		return new JdbcTransactionManager(batchDataSource);
	}*/

	@Autowired
	private DataSource dataSource;

	@Bean
	public Job sampleJob(JobRepository jobRepository, Step sampleStep) {
		return new JobBuilder("sampleJob", jobRepository)
				.start(sampleStep)
				.build();
	}

	/**
	 * Note the TransactionManager is typically autowired in and not needed to be explicitly
	 * configured
	 */
	@Bean
	public Step sampleStep(JobRepository jobRepository,
						   PlatformTransactionManager transactionManager) {
		return new StepBuilder("sampleStep", jobRepository)
				.<Person, Person>chunk(10, transactionManager)
				.reader(itemReader())
				.processor(itemProcessor())
				.writer(itemWriter())
				.build();
	}

	@Bean
	public ItemReader<Person> itemReader() {
		JdbcCursorItemReader<Person> reader = new JdbcCursorItemReader<>();
		reader.setDataSource(dataSource);
		reader.setSql("SELECT name,age FROM xrb_user");
		reader.setRowMapper(new BeanPropertyRowMapper<>(Person.class));
		return reader;
	}

	@Bean
	public ItemProcessor<Person, Person> itemProcessor() {
		return person -> {
			person.setAge(person.getAge() + 1);
			return person;
		};
	}

	@Bean
	public ItemWriter<Person> itemWriter() {
		return new CustomJdbcBatchItemWriter(dataSource);
	}


}